Skip to content

Test: add unit tests for ClinicController#175

Merged
TatjanaTrajkovic merged 2 commits into
mainfrom
test/unit_tests_clinic_controller
Apr 10, 2026
Merged

Test: add unit tests for ClinicController#175
TatjanaTrajkovic merged 2 commits into
mainfrom
test/unit_tests_clinic_controller

Conversation

@TatjanaTrajkovic

@TatjanaTrajkovic TatjanaTrajkovic commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • Tests
    • Added end-to-end test coverage for the clinic creation endpoint.
    • Verifies an HTTP POST to create a clinic returns 200 and the response JSON includes the submitted name, address, and phone number.
    • Confirms the backend creation flow is invoked as part of the request handling.

@coderabbitai

coderabbitai Bot commented Apr 8, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: aa19c2a4-3665-4b88-86ec-ce9f57551032

📥 Commits

Reviewing files that changed from the base of the PR and between f97bb9a and dadd959.

📒 Files selected for processing (1)
  • src/test/java/org/example/vet1177/controller/ClinicControllerTest.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/test/java/org/example/vet1177/controller/ClinicControllerTest.java

📝 Walkthrough

Walkthrough

Added a new Spring MVC web-layer test class ClinicControllerTest that uses @WebMvcTest, MockMvc, and a mocked ClinicService to POST JSON to /api/clinics, assert HTTP 200, verify returned JSON fields, and confirm the service create(...) invocation.

Changes

Cohort / File(s) Summary
ClinicController Tests
src/test/java/org/example/vet1177/controller/ClinicControllerTest.java
New JUnit test with @WebMvcTest and @AutoConfigureMockMvc(addFilters = false), mocking ClinicService (via @MockitoBean), stubbing clinicService.create(...), performing POST /api/clinics with JSON, asserting 200 and returned JSON fields, and verifying service call.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Possibly related PRs

Suggested labels

test

Suggested reviewers

  • lindaeskilsson
  • johanbriger
  • annikaholmqvist94

Poem

🐰 I hopped through code to run a test so bright,
MockMvc hummed and JSON fields took flight,
Clinics checked with care and cheerful cheer,
The rabbit nods — the green bar's here! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and directly describes the main change: adding unit tests for the ClinicController class, which matches the new test file introduced in the pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test/unit_tests_clinic_controller

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@TatjanaTrajkovic TatjanaTrajkovic linked an issue Apr 8, 2026 that may be closed by this pull request

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/test/java/org/example/vet1177/controller/ClinicControllerTest.java (1)

28-51: Add one validation-failure test for @Valid request constraints.

Current coverage is only the happy path. Add a bad-request case (e.g., blank name) to verify controller validation behavior.

Suggested additional test
+    `@Test`
+    void shouldReturnBadRequestWhenNameIsBlank() throws Exception {
+        String json = """
+        {
+            "name": "",
+            "address": "Street",
+            "phoneNumber": "123"
+        }
+        """;
+
+        mockMvc.perform(post("/api/clinics")
+                        .contentType("application/json")
+                        .content(json))
+                .andExpect(status().isBadRequest());
+    }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/test/java/org/example/vet1177/controller/ClinicControllerTest.java`
around lines 28 - 51, Add a new test in ClinicControllerTest that posts an
invalid Clinic JSON (e.g., "name": "" or missing) to the same "/api/clinics"
endpoint to assert validation fails: perform
mockMvc.perform(post("/api/clinics")...).contentType("application/json").content(invalidJson))
and expect status().isBadRequest(); also verify clinicService.create(...) was
not invoked (use verify(clinicService, never()).create(...)) to ensure the
controller's `@Valid` constraints short-circuit processing. Place this alongside
shouldCreateClinic() and name it something like
shouldReturnBadRequestWhenNameBlank().
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/test/java/org/example/vet1177/controller/ClinicControllerTest.java`:
- Line 33: The test currently stubs clinicService.create with
when(clinicService.create(any(), any(), any())).thenReturn(clinic), which masks
incorrect argument mapping; change the stubbing to use exact expected values
(e.g., when(clinicService.create(eq(expectedName), eq(expectedAddress),
eq(expectedOwner))).thenReturn(clinic)) or remove the stub and use an
ArgumentCaptor to capture the actual arguments passed to clinicService.create in
ClinicControllerTest, then assert those captured values equal the expected
request fields and add verify(clinicService).create(...) to ensure it was
invoked once with the correct arguments.

---

Nitpick comments:
In `@src/test/java/org/example/vet1177/controller/ClinicControllerTest.java`:
- Around line 28-51: Add a new test in ClinicControllerTest that posts an
invalid Clinic JSON (e.g., "name": "" or missing) to the same "/api/clinics"
endpoint to assert validation fails: perform
mockMvc.perform(post("/api/clinics")...).contentType("application/json").content(invalidJson))
and expect status().isBadRequest(); also verify clinicService.create(...) was
not invoked (use verify(clinicService, never()).create(...)) to ensure the
controller's `@Valid` constraints short-circuit processing. Place this alongside
shouldCreateClinic() and name it something like
shouldReturnBadRequestWhenNameBlank().
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9fbd61a6-85d1-46ca-9f33-a07b3f647461

📥 Commits

Reviewing files that changed from the base of the PR and between e652c86 and f97bb9a.

📒 Files selected for processing (1)
  • src/test/java/org/example/vet1177/controller/ClinicControllerTest.java

Comment thread src/test/java/org/example/vet1177/controller/ClinicControllerTest.java Outdated
…e mapping

 Use explicit argument matching instead of any() and verify that the controller invokes ClinicService with the correct values.

@annikaholmqvist94 annikaholmqvist94 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mergaaa

@TatjanaTrajkovic
TatjanaTrajkovic merged commit 87c420d into main Apr 10, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test: Add Unit test for ClinicController

2 participants